__author__ = 'aminmahpour'

import re


import os
file = open(os.path.join(os.getcwd(),"Data","bidir1000.fasta"), "r")

f = file.read().lower()
f= f.replace("\n>","|")
f=f.replace("\n","")
feature =f.split("|")

motif=0
gene=0
mx="t\wtcgcga\wa"
coordinates = []
for j in feature:
    #raw_input()
    m = re.findall(mx, j)
    if m is not None and len(m) <> 0:
        for k in m:
            #print j
            motif+=1
        c = j.split(" ")
        m2 = re.search("chr(\w+)_(\d+)_(\d+)", j)
        l =[]
        l.append(m2.group(1))

        l.append(int(m2.group(2)))
        l.append(int(m2.group(3)))
        coordinates.append(l)
        print j
        gene+=1

out = open(os.path.join(os.getcwd(),"Export","bidir300numbers.txt"), "a")
outbed = open(os.path.join(os.getcwd(),"Export","bidir300_motif.bed"), "w")

#o=out.readlines()
#out.writelines(o)
out.write ("%s\t%d\t%d\n" % (mx,motif,gene))
out.close()

for i in coordinates:
    outbed.write("chr%s\t%d\t%d\n" % (i[0].upper(),i[1],i[2]))

print  motif
print gene
